// pages/main/available house/available.js
const url = "http://zbnczy.com/image/sharehouse/"
Page({
  /**
   * 页面的初始数据
   */

  data: {
    twons: ["所有", "海流图乡", "庙东营"],
    villages: ["所有"],
    areas: ["所有", "0~300", "300~600", "600~900", "900以上"],
    priceIndex: 0,
    price: ["1000-2000", "2000-3000", "3000-5000", "5000-10000"],
    houseArray: [{
        id: 1,
        owner: "张北村夏·冬野民宿",
        phone: "5218862",
        town: "张北县台路沟乡",
        village: "大圪垯村",
        title: "张北县台路沟乡大圪垯村民宿",
        basic: "无线WIFI免费\n免费24小时前台服务\n吹风机\n24小时热水",
        kitchen: "",
        room: "一间客厅 一间卧室 一间卫生间",
        area: 25,
        price: null,
        bath: "独立卫生间\n独立淋浴间\n吹风机\n24小时热水\n拖鞋",
        warmth: "空调",
        struct: "砖木",
        type: "个人",
        defaultSort: 1,
        images: [
          "http://zbnczy.com/image/sharehouse/dagada3.png",
          "http://zbnczy.com/image/sharehouse/dagada1.png",
          "http://zbnczy.com/image/sharehouse/dagada2.png",
          "http://zbnczy.com/image/sharehouse/dagada4.png",
          "http://zbnczy.com/image/sharehouse/dagada5.png"
        ],
        videoes: []
      },
      {
        id: 2,
        owner: "张北七分小院",
        phone: "5218862",
        town: "张北县",
        village: "南壕堑",
        title: "张北镇南壕堑“7分小院”",
        basic: "无线WIFI免费\n免费24小时前台服务\n吹风机\n24小时热水",
        kitchen: '',
        room: "两层高落地玻璃，一楼客厅，二楼卧室",
        area: 60,
        price: '',
        bath: "独立卫生间\n独立淋浴间\n吹风机\n24小时热水\n拖鞋",
        warmth: "空调",
        struct: "砖木",
        type: "个人",
        defaultSort: 2,
        images: [
          "http://zbnczy.com/image/sharehouse/qifen9.jpg",
          "http://zbnczy.com/image/sharehouse/qifen1.jpg",
          "http://zbnczy.com/image/sharehouse/qifen2.jpg",
          "http://zbnczy.com/image/sharehouse/qifen3.jpg",
          "http://zbnczy.com/image/sharehouse/qifen4.jpg",
          "http://zbnczy.com/image/sharehouse/qifen5.jpg",
          "http://zbnczy.com/image/sharehouse/qifen6.png",
          "http://zbnczy.com/image/sharehouse/qifen7.jpg",
          "http://zbnczy.com/image/sharehouse/qifen8.jpg",
          "http://zbnczy.com/image/sharehouse/qifen10.jpg"
        ],
        videoes: []
      }
    ],
    searchContext: "",
    maxPages: 22, //最大页码
    pagesNum: 1,
    tyindex: 1,
    frontPage: false,
    lastPage: true
  },
  JumpTo(houseinfo) {
    wx.navigateTo({
      url: `/pages/main/sharedhouse/info/info`,
      success: function (res) {
        let houseInfo = houseinfo.currentTarget.dataset.houseinfo
        console.log(houseinfo);
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('shareInfo', {
          houseInfo
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  request(url, method = "GET") {
    return new Promise(
      (resolve, reject) => {
        wx.request({
          url,
          success: (res) => {
            resolve(res);
          },
          fail: (e) => {
            console.log(e);
          }
        })
      }
    )
  },
  onLoad: function (options) {
    this.request('').then((res) => {
      this.setData({
        houseArray: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  pagesFn: function (e) {
    let type = e.currentTarget.dataset.type;
    let number = e.currentTarget.dataset.number;
    if (typeof type == "string") { //上下页
      if (type == "previous_page") { //上一页
        if (this.data.pagesNum - 1 < 1) {
          return false
        }
        if ((this.data.pagesNum - 1) % 5 == 0) {
          if ((this.data.pagesNum - 6) <= 1) {
            this.setData({
              lastPage: true,
              frontPage: false,
            })
          }
          this.setData({
            lastPage: true,
            tyindex: this.data.pagesNum - 5,
            pagesNum: this.data.pagesNum - 1,
          })
        } else {
          this.setData({
            pagesNum: this.data.pagesNum - 1,
          })
        }
      } else if (type == "next_page") { //下一页
        if (this.data.pagesNum + 1 > this.data.maxPages) {
          return false
        }
        if (this.data.pagesNum % 5 == 0) {
          console.log(this.data.pagesNum + 5)
          console.log(this.data.maxPages)
          if ((this.data.pagesNum + 5) >= 5) {
            this.setData({
              lastPage: true,
              frontPage: true,
            })
          }
          if ((this.data.pagesNum + 5) > this.data.maxPages) {
            this.setData({
              lastPage: false
            })
          }
          this.setData({
            tyindex: this.data.pagesNum + 1,
            pagesNum: this.data.pagesNum + 1,
          })
        } else {
          this.setData({
            pagesNum: this.data.pagesNum + 1,
          })
        }
      } else if (type == "start") { //第一页
        this.setData({
          pagesNum: 1,
          tyindex: 1,
          lastPage: true,
          frontPage: false,
        })
      } else { //最后一页
        if ((this.data.maxPages % 5) == 0) {
          this.setData({
            pagesNum: this.data.maxPages,
            tyindex: this.data.maxPages - 4,
            lastPage: false,
            frontPage: true,
          })
        } else {
          this.setData({
            pagesNum: this.data.maxPages,
            tyindex: this.data.maxPages + 1 - (this.data.maxPages % 5),
            lastPage: false,
            frontPage: true,
          })
        }
      }
    } else {
      this.setData({
        pagesNum: number
      })
    }
    console.log(this.data.pagesNum)
    // this.DataFn()可在此调用数据函数
  },
})